﻿using Geeno.WEBUI.Models;

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Net.Http;
using System.IO;
using MiniExcelLibs;
using MiniExcelLibs.Attributes;

namespace Geeno.WEBUI.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        readonly HttpClient _httpClient = new HttpClient();
        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }

        public IActionResult Index()
        {
            try
            {
                var a = 1;
                var b = 0;
                var c = a / b;
            }
            catch (Exception ex)
            {
                _logger.LogInformation(ex,"测试异常信息");

            }
            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }

        public IActionResult ExportDemo()
        {
            var values = new List<Student>
            {
                new Student { ID=1, Age=10, StuName="jack"},
                new Student { ID=2, Age=102, StuName="2jack"},
            };

            var memoryStream = new MemoryStream();
            memoryStream.SaveAs(values);
            memoryStream.Seek(0, SeekOrigin.Begin);
            return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                FileDownloadName = "demo.xlsx"
            };
        }
        public class Student
        {
            [ExcelColumnName("序号")]
            public int ID { get; set; }
            [ExcelColumnName("年龄")]
            public int Age { get; set; }
            [ExcelColumnName("姓名")]
            public string StuName { get; set; }
        }
    }
}
